package com.xiaojie.context;

import com.google.common.collect.Maps;
import com.xiaojie.parse.model.DataModel;
import com.xiaojie.progress.ExportImportProgress;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Tolerate;

import java.io.File;
import java.util.Map;

/**
 * 获取数据信息
 *
 * @author wangye
 * @classname DataInfoHelper
 * @date 2021/10/8 14:27
 **/
@Data
@Builder
public class DataInfoContext {

    private static ThreadLocal<DataInfoContext> threadLocal = new ThreadLocal<>();

    //获取表中字段新数据与旧数据对应关系，{tableName:{fieldName:{oldValue:newValue},...},....}
    private Map<String, Map<String, Map>> correspondDataTableFieldMap = Maps.newHashMap();
    //配置文件模型
    private DataModel dataModel;
    //进度信息
    private ExportImportProgress progress;
    //记录表中替换字段旧值与新值的对应关系{tableName:{fieldName:{oldFieldValue:newFieldValue,...},...},...}
    private Map<String, Map<String, Map>> refFieldValueMap = Maps.newHashMap();
    //通过参数传入的字段替换的新值
    private Map<String, Object> newFieldValueParams;
    //导入数据时产生的临时目录
    private String importTempDir;
    //压缩包解压后文件内容
    private Map<String, File> unzipFileMap;


    //获取当前线程DataInfoContext
    public static DataInfoContext get() {
        DataInfoContext dataInfoContext = threadLocal.get();
        if (dataInfoContext == null) {
            dataInfoContext = new DataInfoContext();
            threadLocal.set(dataInfoContext);
        }
        return dataInfoContext;
    }

    //清除当前线程DataInfoContext
    public static void remove() {
        threadLocal.remove();
    }


    public void setDataModelInfo(DataModel dataModel) {
        if (this.dataModel == null) {
            this.dataModel = dataModel;
        }
    }

    public DataModel getDataModelInfo() {
        return dataModel;
    }


    public Map<String, File> getUnzipFileMap() {
        return unzipFileMap;
    }

    public void setUnzipFileMap(Map<String, File> unzipFileMap) {
        this.unzipFileMap = unzipFileMap;
    }

    @Tolerate
    public DataInfoContext() {
    }
}
